<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/3/11
 * Time: 16:04
 */

namespace iBrand\Component\Purchase\Repositories\Eloquent;


use iBrand\Component\Purchase\Models\Purchase;
use iBrand\Component\Purchase\Repositories\PurchaseRepository;
use Prettus\Repository\Eloquent\BaseRepository;

class PurchaseRepositoryEloquent extends BaseRepository implements PurchaseRepository
{
    public function model()
    {
        return Purchase::class;
    }

    public function getPurchasePaginate($where = [], $time = [], $limit = 10)
    {
        return $this->scopeQuery(function ($query) use ($where, $time) {
            $query = $query->Where(function ($query) use ($where, $time) {
                if (count($where)) {
                    foreach ($where as $key => $value) {
                        if (is_array($value)) {
                            list($operate, $va) = $value;
                            $query = $query->where($key, $operate, $va);
                        } else {
                            $query = $query->where($key, $value);
                        }
                    }
                }

                if (count($time)) {
                    foreach ($time as $key => $value) {
                        if (is_array($value)) {
                            list($operate, $va) = $value;
                            $query = $query->where($key, $operate, $va);
                        } else {
                            $query = $query->where($key, $value);
                        }
                    }
                }

            });
            return $query->orderBy('created_at', 'desc');
        })->with('clerk')->paginate($limit);
    }
}